<?
require VIEW.'view_utility.php';

/* ensure legel access, davee 2011-10-10 */
$username=''; $userid=0;
if(isset($_COOKIE["userid"]) && $_COOKIE["userid"]!="" ) $userid=decrypt($_COOKIE["userid"]);
if($userid==0){
	echo '<script>window.top.location="login.php"</script>'; exit;
}
if(isset($_COOKIE["username"]) && $_COOKIE["username"]!="" ) $username=decrypt($_COOKIE["username"]);

// 扫描取消过期未付款订单 scan invalid orders
// 最早时间
$fromtime = mktime(date('H')-ORDER_VALID_PERIOD,0,0,date("m"),date("d"),date("Y"));
$fromdate = date('Y-m-d H:i:s',$fromtime);
$where=array();
//$where[]=array('name'=>'customer_id','val'=>$customer_id);
$where[]=array('name'=>'order_status','val'=>2, 'opn'=>'<');
$where[]=array('name'=>'payment_method_id','val'=>5, 'opn'=>'!=');
$where[]=array('name'=>'created_time','val'=>$fromdate, 'opn'=>'<');
$tem=$sales_order->get_rows_advanced(10240,$where);
$in_array=array();
$in_array_order=array();
foreach($tem as $val){ $in_array[]=$val['id'];
$in_array_order[]=$val['ref_no'];}
$arr=array('order_status'=>'8'); // 系统取消状态
try {
	$db->begin_transaction();
	$sales_order->update($arr,'id',$in_array);
	// 恢复库存
	foreach ($in_array_order as $val) {
		$product->restore_stock_by_order($val);
	}
	$db->commit();
}
catch(PDOException $ex){
	//Something went wrong rollback!
	$db->rollBack();
	pre($ex->getMessage());
} 

// language file 语言文件
include '../lang/'.$lang.'.php';

//end
//echo memory_get_usage();